package com.salon.client.tabItems;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.salon.client.GreetingServiceAsync;
import com.salon.client.Salon;
import com.salon.client.beans.IncomeMaterialsBean;
import com.salon.client.beans.MaterialBean;
import com.salon.client.beans.ParentPagingLoadResult;
import com.salon.client.widgets.MaterialDialog;

public class CurrentMaterialTabItemPanel extends ParentTabItemPanel<IncomeMaterialsBean> {

	public CurrentMaterialTabItemPanel(String title) {
		super(title);
	}

	@Override
	ColumnModel getColomnModel() {
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();

		ColumnConfig conf = new ColumnConfig("code", "Код", 50);
		configs.add(conf);
		
		conf = new ColumnConfig("name", "Наименование", 100);
		configs.add(conf);
		
		conf = new ColumnConfig("count", "Количество", 50);
		conf.setRenderer(getCountedRenderer());
		configs.add(conf);
		
		conf = new ColumnConfig("id", "Единица прихода", 50);
		conf.setRenderer(getIsCountedRenderer());
		configs.add(conf);
		
		return new ColumnModel(configs);
	}
	
	@Override
	protected void initComponents() {
		addButton("Пересчитать", "brit-add-user", new SelectionListener<ButtonEvent>() {
			
			public void componentSelected(ButtonEvent ce) {
				((GreetingServiceAsync) Registry.get(Salon.SERVICE))
				.calcCurrentMaterial(new AsyncCallback<Void>() {

							@Override
							public void onFailure(Throwable caught) {
								Info.display("Не удалось", "пересчитать товара");
								
							}

							@Override
							public void onSuccess(Void result) {
								Info.display("Пересчет товаров", "успешно");
								
							}
					
				});
			}
			
		});
		/*
		addButton("Редактировать материал", "brit-add-user", new SelectionListener<ButtonEvent>() {
			
			public void componentSelected(ButtonEvent ce) {
				MaterialDialog dlg = new MaterialDialog(MaterialTabItemPanel.this, selectedBean);
				dlg.show();
			}
			
		});
		
		addButton("Удалить материал", "brit-delete-user", new SelectionListener<ButtonEvent>() {
			
			public void componentSelected(ButtonEvent ce) {
				if (selectedBean != null) {
					((GreetingServiceAsync) Registry.get(Salon.SERVICE))
					.deleteItem("material", selectedBean.getId(), 
							new AsyncCallback<Void>() {

								@Override
								public void onFailure(Throwable caught) {
									Info.display("Не удалось", "удалить материал");
									
								}

								@Override
								public void onSuccess(Void result) {
									Info.display("Удаление материала", "успешно");
									MaterialTabItemPanel.this.refreshStore();
									
								}
						
					});
				}
			}
		});*/
		super.initComponents();
	}
	
	@Override
	RpcProxy<ParentPagingLoadResult<IncomeMaterialsBean>> getProxy() {
		// TODO Auto-generated method stub
		RpcProxy<ParentPagingLoadResult<IncomeMaterialsBean>> proxy = new RpcProxy<ParentPagingLoadResult<IncomeMaterialsBean>>() {
			@Override
			protected void load(Object loadConfig,
					AsyncCallback<ParentPagingLoadResult<IncomeMaterialsBean>> callback) {
				PagingLoadConfig ploadConfig = (PagingLoadConfig) loadConfig;
				int offset = ploadConfig.getOffset();
				((GreetingServiceAsync) Registry.get(Salon.SERVICE))
						.loadCurrentMaterials(offset, ploadConfig.getLimit(), null, 
								callback);

			}
		};
		
		return proxy;
	}
	
	GridCellRenderer<BeanModel> isCountedRenderer = null;

	public GridCellRenderer<BeanModel> getIsCountedRenderer() {
		if (isCountedRenderer == null) {
			isCountedRenderer = new GridCellRenderer<BeanModel>() {

				@Override
				public Object render(BeanModel model, String property,
						ColumnData config, int rowIndex, int colIndex,
						ListStore<BeanModel> store, Grid<BeanModel> grid) {
					IncomeMaterialsBean materialBean = model.getBean();
					if (materialBean != null) {
						return materialBean.getContedAsString();
					}
					return "";
				}
			};
		}
		return isCountedRenderer;
	}
	
	GridCellRenderer<BeanModel> сountedRenderer = null;

	public GridCellRenderer<BeanModel> getCountedRenderer() {
		if (сountedRenderer == null) {
			сountedRenderer = new GridCellRenderer<BeanModel>() {

				@Override
				public Object render(BeanModel model, String property,
						ColumnData config, int rowIndex, int colIndex,
						ListStore<BeanModel> store, Grid<BeanModel> grid) {
					IncomeMaterialsBean materialBean = model.getBean();
					if (materialBean != null) {
						return materialBean.getCountWithString();
					}
					return "";
				}
			};
		}
		return сountedRenderer;
	}
}